package com.hit.server.biz.controller;

import cn.hutool.core.util.ArrayUtil;
import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hit.common.core.util.HitConvertUtils;
import com.hit.common.core.util.R;
import com.hit.common.log.annotation.SysLog;
import com.hit.server.api.entity.RobotEntity;
import com.hit.server.api.dto.RobotQueryDTO;
import com.hit.server.api.dto.RobotSaveDTO;
import com.hit.server.api.dto.RobotUpdateDTO;
import com.hit.server.api.vo.RobotVO;
import com.hit.server.api.service.RobotService;
import com.hit.common.excel.annotation.ResponseExcel;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import org.springdoc.core.annotations.ParameterObject;
import org.springframework.http.HttpHeaders;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Objects;

/**
 * 机器人
 *
 * @author kehanjiang
 * @date 2025-04-03 17:24:22
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/robot" )
@Tag(description = "robot" , name = "机器人管理" )
@SecurityRequirement(name = HttpHeaders.AUTHORIZATION)
public class RobotController {

    private final  RobotService robotService;

    /**
     * 分页查询
     * @param page 分页对象
     * @param dto 机器人
     * @return
     */
    @Operation(summary = "分页查询" , description = "分页查询" )
    @GetMapping("/page" )
    public R<Page<RobotVO>> getRobotPage(@ParameterObject Page page, @ParameterObject RobotQueryDTO dto) {
	    dto = HitConvertUtils.emptyToNull(dto);
        return R.ok(robotService.getRobotPage(page, dto));
    }


    /**
     * 通过id查询机器人
     * @param id id
     * @return R
     */
    @Operation(summary = "通过id查询" , description = "通过id查询" )
    @GetMapping("/{id}" )
    public R<RobotVO> getById(@PathVariable("id" ) Long id) {
        return R.ok(robotService.getRobotById(id));
    }

    /**
     * 新增机器人
     * @param dto 机器人
     * @return R
     */
    @Operation(summary = "新增机器人" , description = "新增机器人" )
    @SysLog("新增机器人" )
    @PostMapping
    public R<Boolean> save(@RequestBody RobotSaveDTO dto) {
        return R.ok(robotService.saveRobot(dto));
    }

    /**
     * 修改机器人
     * @param dto 机器人
     * @return R
     */
    @Operation(summary = "修改机器人" , description = "修改机器人" )
    @SysLog("修改机器人" )
    @PutMapping
    public R<Boolean> updateById(@RequestBody RobotUpdateDTO dto) {
        return R.ok(robotService.updateRobotById(dto));
    }

    /**
     * 通过id删除机器人
     * @param ids id列表
     * @return R
     */
    @Operation(summary = "通过id删除机器人" , description = "通过id删除机器人" )
    @SysLog("通过id删除机器人" )
    @DeleteMapping
    public R<Boolean> removeById(@RequestBody Long[] ids) {
        return R.ok(robotService.removeRobotBatchByIds(CollUtil.toList(ids)));
    }


    /**
     * 导出excel 表格
     * @param robot 查询条件
   	 * @param ids 导出指定ID
     * @return excel 文件流
     */
    @ResponseExcel
    @GetMapping("/export")
    public List<RobotEntity> export(RobotEntity robot,@RequestParam List<Long> ids) {
        return robotService.list(Wrappers.lambdaQuery(robot).in(ArrayUtil.isNotEmpty(ids), RobotEntity::getId, ids));
    }
}